package model

import (
	"hertz/demo/biz/db"

	"gorm.io/gorm"
)

func MigrateDB(db *gorm.DB) {
	// Migrate the schema
	tables := []interface{}{
		&User{},
		&Sample{},
		&Selection{},
		&Media{},
		&Label{},
		&Job{},
		&Mission{},
		//		&Collection{},
		//		&Report{},
	}

	err := db.AutoMigrate(tables...)
	if err != nil {
		panic("auto db migration failed:" + err.Error())
	}
}

func ReleaseDB(db *gorm.DB) {
	if db != nil {
		sql, err := db.DB()
		if err != nil {
			panic("get sql failed")
		}
		sql.Close()
	}
}

func InitTestDB() *gorm.DB {
	dbPath := "file::memory:?cache=shared"
	ret, err := db.OpenDB(dbPath)
	if err != nil {
		panic("failed to connect database")
	}

	MigrateDB(ret)

	return ret
}
